package com.itboye.ihomebank.activity.qianyue;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.mobileim.lib.model.message.IFileMessage;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.OrderViewPagerAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.activity.qianyue.fragment.OrderFragment;
import com.itboye.ihomebank.activity.qianyue.fragment.YiWanChengFragment;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.broad.RedDotEnum;
import com.umeng.message.common.Const;

import java.util.ArrayList;

/**
 * 我的签约
 */
public class ActivityMySign extends BaseOtherActivity implements
		OnClickListener {
	View v_statusbar;
	TextView add_shap_title_tv;
	ImageView close_icon;
	private ViewPager viewpager;
	private ArrayList<Fragment> fragmentList;
	private TextView tv_guid1, tv_guid2;
//	tv_guid3, tv_guid4;
	private ImageView cursor;
	private int currIndex;// 当前页卡编号
	private float offset;// 图片移动的偏移量
	private int index;

	private ImageView contract_redDot;// 红点
	
	public static String ONE = "one";
	public static int ONRESTART=0;//刷新fragment列表
	@Override
	protected int layout_id() {
		return R.layout.fragment_qianyue;
	}

	@Override
	protected void onInitController() {

	}
    public BroadcastReceiver redReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
		isShowRedDot();//是否显示红点
		}
	};
	@Override
	protected void onInit() {
		ONRESTART=0;
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("签约管理");
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		IntentFilter intentFilter= new IntentFilter("showRedDot");
		registerReceiver(redReceiver,intentFilter);
		index = 0;
        isShowRedDot();
		initTextView();
		initImage();
		initViewPager();

	}
    // 是否显示红点
	public void isShowRedDot(){
		boolean fuJianOrder = (boolean) SPUtils.get(getApplication(), null, SPContants.FUJIAN_ORDER, false);
		if (fuJianOrder){//有附件单未填写
			contract_redDot.setVisibility(View.VISIBLE);
		}else { //无附件单
			contract_redDot.setVisibility(View.GONE);
		}

	}

	@Override
	protected void onRestart() {
		super.onRestart();
		ONRESTART=1;
		Log.d("fragment==","shuaxin");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	BroadcastReceiver redDot= new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d("ActivityMySign===","shoudaole");
			boolean fuJianOrder = intent.getBooleanExtra("FuJianOrder", false);


		}
	};
	/**
	 * 初始化标签
	 */
	public void initTextView() {
		tv_guid1.setOnClickListener(new txListener(0));
		tv_guid2.setOnClickListener(new txListener(1));
	}

	/**
	 * 初始化图片的位移像素
	 */
	public void initImage() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int screenH = dm.heightPixels;
		offset = screenW / 2.0f;
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				(int) offset, 4);
		cursor.setLayoutParams(params);
	}

	/**
	 * 初始化ViewPager
	 */
	public void initViewPager() {
		fragmentList = new ArrayList<Fragment>();
		Fragment fragment01 = new OrderFragment(0);
		Fragment fragment02 = new YiWanChengFragment(1);
		fragmentList.add(fragment01);
		fragmentList.add(fragment02);

		// //给ViewPager设置适配器
		viewpager.setAdapter(new OrderViewPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		viewpager.setCurrentItem(0);// 设置当前显示标签页为第一页
		tranSlate(0, 0);
		viewpager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器
	}

	public class txListener implements OnClickListener {
		private int index = 0;

		public txListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			viewpager.setCurrentItem(index);
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {
		private float one = offset;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub

			tranSlate(arg0, 200);
			currIndex = arg0;
			
			if(arg0==0){
				//Toast.makeText(MyApplcation.ctx, "第1个", 0).show();
			}
			if(arg0==1){
				//Toast.makeText(MyApplcation.ctx, "第2个", 0).show();
			}
			if(arg0==2){
				//Toast.makeText(MyApplcation.ctx, "第3个", 0).show();
			}
			if(arg0==3){
				//Toast.makeText(MyApplcation.ctx, "第4个", 0).show();
			}
		}
	}

	/**
	 * 平移动画
	 * 
	 * @param currentItem
	 * @param durationMillis
	 */
	public void tranSlate(int currentItem, long durationMillis) {
		float one = offset;
		Log.v("offest", "" + offset);
		Animation animation = new TranslateAnimation(currIndex * one,
				currentItem * one, 0, 0);// 平移动画
		Log.v("currentoffest", "" + currIndex * one);
		Log.v("currentItem", "" + currentItem * one);
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(durationMillis);// 动画持续时间0.2秒
		cursor.startAnimation(animation);// 是用ImageView来显示动画的
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}
}
